package org.example.examsystem.controller;


import org.example.examsystem.entity.Exam;
import org.example.examsystem.entity.StudentExam;
import org.example.examsystem.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/exams")
public class ExamController {

    @Autowired
    private ExamService examService;

    @GetMapping
    public Page<Exam> getExams(@RequestParam(defaultValue = "0") int page,
                               @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return examService.getExams(pageable);
    }

    @PutMapping("/{id}")
    public void updateExam(@PathVariable int id, @RequestBody Exam exam) {
        exam.setId(id);
        examService.updateExam(exam);
    }

    @DeleteMapping("/{id}")
    public void deleteExam(@PathVariable int id) {
        examService.deleteExam(id);
    }

    @PostMapping("/batch")
    public void batchDelete(@RequestBody List<Integer> ids) {
        examService.batchDeleteExams(ids);
    }
    //这块控制器应该冲突
    @PostMapping
    public Exam saveExam(@RequestBody Exam exam) {
        return examService.saveExam(exam);
    }

    @GetMapping("/{eid}")
    public Exam getExamById(@PathVariable int eid) {
        return examService.getExamById(eid);
    }
    @GetMapping("/{examId}/details")
    public Exam getExamDetails(@PathVariable Integer examId) {
        return examService.getExamDetailsById(examId);
    }
    //提交考试
    @PostMapping("/{examId}/submit")
    public StudentExam submitExam(@PathVariable Integer examId, @RequestBody List<String> answers) {
        // 这里需要根据实际情况获取用户ID
        Integer userId = 11;
        return examService.submitExam(examId, userId, answers);
    }

    @GetMapping("/{examId}/answered/details")
    public Exam getAnsweredExamDetails(@PathVariable Integer examId) {
        //测试前端传参
//        System.out.println("examId:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+examId);
        return examService.getAnsweredExamDetails(examId);
    }


}

